<!-- 新增/修改目录弹窗 -->
<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" custom-class="common-dialog"
    :close-on-click-modal="false">
    <el-form :model="form" :rules="rules" label-position="top">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="父级目录">
        <treeselect v-model="form.parentId" :options="treeData"></treeselect>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button class="cancel-btn" @click="cancel">取 消</el-button>
      <el-button class="submit-btn" @click="cancel">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      title: "",
      dialogFormVisible: false,
      form: {},
      treeData: [],
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(row, treeData) {
      this.dialogFormVisible = true;
      this.reset(treeData);
      if (!row) {
        this.title = "新增目录";
      } else {
        this.title = "修改目录";
        this.form.name = row.label;

        this.form.parentId = row.parentId;
      }
    },
    reset(treeData) {
      this.treeData = [];
      const menu = {
        id: -1,
        label: "根目录",
        children: [],
      };
      menu.children = this.handleTree(
        JSON.parse(JSON.stringify(treeData)),
        "id"
      );
      this.treeData.push(menu);
      this.form = {
        name: null,
        parentId: null,
      };
    },
    cancel() {
      this.dialogFormVisible = false;
    },
  },
};
</script>
